/*
 * Created by Wanghw 2018/4/24.
 */

package com.fenggo.platform.account.exception;

import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

@Data
public class AppException extends RuntimeException{

    /**
     * 账务中心所有异常编码60开头。
     *
     * 请各位组员自觉遵守！！！
     */
    protected static final String PRE="60";

    protected Map<String,String> definedMapping(){
        return null;
    }

    /**
     * 跨服务调用异常转换
     * @param sourCode
     * @return
     */
    protected String convertErrorCode(String sourCode){
        Map<String,String> mapping = definedMapping();
        if (mapping!=null && StringUtils.isNotEmpty(sourCode) && mapping.keySet().contains(sourCode)){
            return mapping.get(sourCode);
        }
        return sourCode;
    }

    protected String errorCode;
    protected String errorMsg;
    protected Object data;

    public AppException(String errorCode, String errorMsg){
        super(errorMsg);
        this.errorCode = convertErrorCode(errorCode);
        this.errorMsg = errorMsg;
    }

    public AppException(String errorCode, String errorMsg,Object data){
        super(errorMsg);
        this.errorCode = convertErrorCode(errorCode);
        this.errorMsg = errorMsg;
        this.data=data;
    }
}
